package easy;

/**
 * Created by daodao on 2018/7/16.
 * Problem 83
 * Description: Given a sorted linked list, delete all duplicates such that each element appear only once.
 * <p>
 * Example 1: Input: 1->1->2
 * Output: 1->2
 * Example 2: Input: 1->1->2->3->3
 * Output: 1->2->3
 */
public class RemoveDuplicatesFromSortedList {
    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            ListNode p = this;
            while (p != null && p.next!= null) {
                sb.append(p.val).append("->");
                p = p.next;
            }
            return sb.append(p.val).toString();
        }
    }

    public static ListNode deleteDuplicates(ListNode head) {
        ListNode p = head;
        while (p != null && p.next != null) {
            if (p.next.val == p.val) {
                ListNode temp = p.next.next;
                p.next = temp;
                continue;
            }
            p = p.next;
        }
        return head;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(1);
        head.next.next = new ListNode(2);
        head.next.next.next = new ListNode(3);
        head.next.next.next.next = new ListNode(3);
        System.out.println(head);
        System.out.println(deleteDuplicates(head));
    }
}
